<?php
namespace Admin\Controller;

use Admin\Controller\CommonController;

/**
 * 栏目相关模块
 * @author wangdong
 */
class CategoryController extends CommonController
{
    /**
     * 栏目管理
     */
    public function categoryList()
    {
        $this->display('category_list');
    }

    public function categoryListData()
    {
        if (S('category_categorylist')) {
            $data = S('category_categorylist');
        } else {
            $category_db = D('Category');
            $data = $category_db->getTree();
            S('category_categorylist', $data);
        }
        $this->ajaxReturn($data);
    }

    /**
     * 添加栏目
     */
    public function categoryAdd()
    {
        if (IS_POST) {
            $category_db = D('Category');
            $data = I('post.info');
//    		$data['ismenu'] = $data['ismenu'] ? '1' : '0';
            $id = I('post.id');
            if (!empty($id)) {
                $res = $category_db->where(array('catid' => $id))->save($data);
                $msgType = "修改";
            } else {
                $res = $category_db->add($data);
                $msgType = "添加";
            }
            if ($res) {
                $category_db->clearCatche();
                $this->success($msgType . '成功');
            } else {
                $this->error($msgType . '失败');
            }
        } else {
            $id = I('get.id');
            $parentid = I('get.parentid');
            $this->assign('parentid', $parentid);
            if (!empty($id)) {
                $category_db = D('Category');
                $info = $category_db->where(array('catid' => $id))->find();
                $this->assign('parentid', $info['parentid']);
                $this->assign('info', $info);
            }
            $this->assign('typelist', C('CONTENT_CATEGORY_TYPE'));
            $this->display('category_add');
        }
    }


    /**
     * 删除栏目
     */
    public function categoryDelete($id = 0)
    {
        if ($id && IS_POST) {
            $category_db = D('Category');
            $result = $category_db->where(array('catid' => $id))->delete();
            if ($result) {
                $category_db->clearCatche();
                $this->success('删除成功');
            } else {
                $this->error('删除失败');
            }
        } else {
            $this->error('无法删除');
        }
    }

    /**
     * 栏目排序
     */
    public function categoryOrder()
    {
        if (IS_POST) {
            $category_db = D('Category');
            $order = I('post.order');
            $id = I('post.id');
            $category_db->where(array('catid' => $id))->save(array('listorder' => $order));
            $category_db->clearCatche();
            $this->success('操作成功');
        } else {
            $this->error('操作失败');
        }
    }

    /**
     * 栏目下拉框
     */
    public function public_categorySelect()
    {
        if (S('category_public_categoryselect')) {
            $data = S('category_public_categoryselect');
        } else {
            $category_db = D('Category');
            $data = $category_db->getSelectTree();
            $data = array(0 => array('id' => 0, 'text' => '作为一级栏目', 'children' => $data));
            S('category_public_categoryselect', $data);
        }
        $this->ajaxReturn($data);
    }
}